package com.example.kaiqiang.dagger2demo

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.android.synthetic.main.item.view.*

class AndroidAdapter(context: Context?, list: List<FuckGoods>?) : RecyclerView.Adapter<AndroidAdapter.ViewHoder>() {
    var mList: List<FuckGoods>? = list
    var linsnter: (FuckGoods) -> Unit = {}

    var mContext: Context? = context
    override fun getItemCount(): Int {
        return if (mList == null || mList!!.isEmpty()) {
            0
        } else {
            mList!!.size
        }
    }


    fun upData(list: List<FuckGoods>?) {
        mList = list
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHoder {
        return ViewHoder(LayoutInflater.from(mContext).inflate(R.layout.item, parent, false), mContext)

    }

    override fun onBindViewHolder(holder: ViewHoder, position: Int) {
        holder.itemView.setOnClickListener {

            linsnter.invoke(mList!![position])

        }
        holder.bindView(mList!![position])
    }


    class ViewHoder(itemView: View?, context: Context?) : RecyclerView.ViewHolder(itemView) {
        var mContext: Context? = context

        fun bindView(entry: FuckGoods) {
            itemView.tv_desc.text = entry.desc
            itemView.tv_who.text = entry.who
            itemView.tv_create.text = entry.create()

            if (entry.hasImg()) {
                itemView.image.visibility = View.VISIBLE
                Glide.with(mContext).load(entry.images[0])//图片的url
                        .asBitmap()
                        .centerCrop()
                        //   .placeholder(DEFAULT_AVATAR) //默认的占位图片
                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                        .into(itemView.image)
            } else {
                itemView.image.visibility = View.GONE
            }
        }

    }


    fun setOnItemClickListener(myli: (FuckGoods) -> Unit) {
        this.linsnter = myli
    }
}